package com.bazaaroid.client.web.gwt.partnermodule.client.view.products;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.ProductDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.products.impl.ProductsTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProductEntryView extends Composite {

	private ProductsTabViewImpl productsTabView;
	private ProductDTO productDTO;
	private TitleCommandBar titleBar;
	private HTML description = new HTML();
	
	public class EditClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			productsTabView.startEdit(ProductEntryView.this);
		}
	}
	
	public class DeleteClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(Window.confirm("Are you sure you wand to delete '" + productDTO.getName() + "'?")) {
				productsTabView.getProductViewHandler().onProductEntryDeleted(productDTO);
				removeFromParent();
			}
		}
	}
	
	public ProductEntryView(ProductsTabViewImpl productsTabView, ProductDTO productDTO) {
		this.productsTabView = productsTabView;
		this.productDTO = productDTO;
		
		VerticalPanel productPanel = new VerticalPanel();
		initWidget(productPanel);
		
		titleBar = new TitleCommandBar(productDTO.getName());
		titleBar.addCommand("edit", new EditClickHandler());
		titleBar.addCommand("delete", new DeleteClickHandler());
		productPanel.add(titleBar);
		productPanel.add(description);
		productPanel.setStyleName("productPanel");
		description.setStyleName("productDescription");
		//titleBar.addStyleName("");
		update();
	}
	
	public void update() {
		titleBar.setTitle(productDTO.getName());
		description.setHTML(productDTO.getDescription());
	}

	public ProductDTO getProductDTO() {
		return productDTO;
	}

	public ProductsTabViewImpl getProductsTabView() {
		return productsTabView;
	}
}
